package com.lxb.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxb.eduservice.entity.EduSubject;
import com.lxb.eduservice.entity.excel.SubjectData;
import com.lxb.eduservice.service.EduSubjectService;
import com.lxb.servicebase.exceptionhandler.GuliException;


/**
 * @author xuebin.li
 * @createTime 2022/10/29 17:05
 */
public class SubjectEasyExcelListener extends AnalysisEventListener<SubjectData> {

    //因为监听器不能被spring管理，监听器是属于servelet容器处理的，需要自己new，不能注入其他对象
    public EduSubjectService subjectService;
    public SubjectEasyExcelListener subjectEasyExcelListener;

    public SubjectEasyExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 一行一行读取文件信息
     *
     * @param subjectData
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        //判断数据是否为空
        if (subjectData == null) {
            throw new GuliException(20001, "文件数据为空");
        }

        //一行一行读取数据
        //判断一级分类是否重复
        EduSubject eduOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());

        //判断一级分类是否有重复
        if(eduOneSubject == null){
            eduOneSubject = new EduSubject();
            eduOneSubject.setTitle(subjectData.getOneSubjectName());
            eduOneSubject.setParentId("0");
            //添加一级分类
            subjectService.save(eduOneSubject);
        }

        //获取二级分类id
        String pid = eduOneSubject.getId();

        //添加二级分类
        //判断二级分类是否重复
        EduSubject eduTwoSubject = this.existTwoSubject(subjectService,subjectData.getTwoSubjectName(),pid);
        if(eduTwoSubject == null){
            eduTwoSubject = new EduSubject();
            eduTwoSubject.setTitle(subjectData.getTwoSubjectName());
            eduTwoSubject.setParentId(pid);
            subjectService.save(eduTwoSubject);
        }
    }

    /**
     * 判断一级分类不可重复
     *
     * @param subjectService
     * @param name
     * @return
     */
    private EduSubject existOneSubject(EduSubjectService subjectService, String name) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    /**
     * 判断二级分类不可重复
     *
     * @param subjectService
     * @param name
     * @return
     */
    private EduSubject existTwoSubject(EduSubjectService subjectService, String name, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
